package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.example.coffeeseller_2.R;

/**
 * Created by WH on 2018/8/13.
 */
public class MyBeiSaiErView extends SurfaceView implements SurfaceHolder.Callback {
    private Context context;
    private SurfaceHolder surfaceHolder;
    private Thread thread;
    private int paintColor;
    private int step = 10;
    private int startColor;
    private int centerColor;
    private int endColor;
    private int measuredWidth, measuredHeight;
    private LinearGradient lg;
    private boolean isStop = false;
    private boolean isDestory = false;

    public MyBeiSaiErView(Context context) {
        super(context);
    }

    public MyBeiSaiErView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray attrsArray = context.obtainStyledAttributes(attrs, R.styleable.MyBeiSaiErView);
        try {
            paintColor = attrsArray.getColor(R.styleable.MyBeiSaiErView_waveColor, ContextCompat.getColor(context, R.color.white));
            step = attrsArray.getInteger(R.styleable.MyBeiSaiErView_step, 10);

            startColor = attrsArray.getColor(R.styleable.MyBeiSaiErView_startColor,
                    Color.WHITE);

            centerColor = attrsArray.getColor(R.styleable.MyBeiSaiErView_centerColor,
                    Color.WHITE);

            endColor = attrsArray.getColor(R.styleable.MyBeiSaiErView_endColor,
                    Color.WHITE);
        }finally {
            attrsArray.recycle();
        }
        surfaceHolder = getHolder();

        //setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);// 设置画布  背景透明
        surfaceHolder.addCallback(this);

        paintColor = ContextCompat.getColor(context, R.color.background_2);

    }

    public MyBeiSaiErView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setStop(boolean stop) {
        isStop = stop;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bng);

        Canvas canvas = holder.lockCanvas();

        if (canvas != null) {

            RectF rectF = new RectF(0, 0, this.getWidth(), this.getHeight());

            canvas.drawBitmap(bitmap, null, rectF, null);

            holder.unlockCanvasAndPost(canvas);

        }

        if (thread == null) {
            isDestory = false;
            thread = new Thread(new MyThread());
            thread.start();
        }
        Log.d("----------", "surfaceCreated");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d("----------", "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d("----------", "surfaceDestroyed");

        if (thread != null) {
            isDestory = true;
            thread = null;

        }
    }

    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            /**
             * 修改波浪的宽度
             */
            measuredWidth = getMeasuredWidth();
            /**
             * 修改波浪的高度
             */
            measuredHeight = getMeasuredHeight();
            /**
             * 总共平移的间隔
             */

            int[] colors = new int[]{
                    startColor, centerColor, endColor
            };
            lg = new LinearGradient(0, 0, measuredWidth, measuredHeight
                    , colors, null, Shader.TileMode.MIRROR);

            int totalWidth = 0;
            int totalWidth2 = 0;

            Paint paint = new Paint();
            paint.setShader(lg);
            paint.setAlpha(150);
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);

            Paint paint1 = new Paint();
            paint1.setShader(lg);
            paint1.setAlpha(100);
            paint1.setAntiAlias(true);
            paint1.setStyle(Paint.Style.FILL);

            while (!isDestory) {
                try {
                    sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (!isStop) {
                    draw(totalWidth, totalWidth2, paint, paint1);

                    totalWidth += 2.2;
                    totalWidth2 += 3.3;

                    if (totalWidth > getMeasuredWidth()) {
                        totalWidth = 0;
                    }


                    if (totalWidth2 > getMeasuredWidth()) {
                        totalWidth2 = 0;
                    }

                }
            }

        }

        public void draw(int totalWidth, int totalWidth2, Paint paint, Paint paint1) {

            Canvas canvas = surfaceHolder.lockCanvas();
            try {
                if (canvas == null) {
                    return;
                }
                canvas.drawColor(paintColor);

                Path path = getSinPath(totalWidth, 3, 5, 1);
                canvas.drawPath(path, paint);

                Path path2 = getSinPath(totalWidth2, 3, 1, 5);
                canvas.drawPath(path2, paint1);

                surfaceHolder.unlockCanvasAndPost(canvas);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

        private Path getPath(int totalWidth) {
            Path path = new Path();

            path.moveTo(-measuredWidth + totalWidth, measuredHeight * 3 / 8);
            path.quadTo(-measuredWidth * 3 / 4 + totalWidth, measuredHeight * 5 / 8,
                    -measuredWidth / 2 + totalWidth, measuredHeight * 3 / 8);
            path.quadTo(-measuredWidth / 4 + totalWidth, measuredHeight * 1 / 8,
                    0 + totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth / 4 + totalWidth, measuredHeight * 5 / 8,
                    measuredWidth / 2 + totalWidth, measuredHeight * 3 / 8);
            path.quadTo(measuredWidth * 3 / 4 + totalWidth, measuredHeight * 1 / 8,
                    measuredWidth + totalWidth, measuredHeight * 3 / 8);

            path.lineTo(measuredWidth + totalWidth, measuredHeight);
            //  if ()
            path.lineTo(-measuredWidth + totalWidth, measuredHeight);
            path.close();
            return path;
        }

        private Path getPath2(int totalWidth) {
            Path path2 = new Path();

            int height = measuredHeight;
            path2.moveTo(-measuredWidth + totalWidth, height * 3 / 8);
            path2.quadTo(-measuredWidth * 3 / 4 + totalWidth, height * 1 / 8,
                    -measuredWidth / 2 + totalWidth, height * 3 / 8);
            path2.quadTo(-measuredWidth / 4 + totalWidth, height * 5 / 8,
                    0 + totalWidth, height * 3 / 8);
            path2.quadTo(measuredWidth / 4 + totalWidth, height * 1 / 8,
                    measuredWidth / 2 + totalWidth, height * 3 / 8);
            path2.quadTo(measuredWidth * 3 / 4 + totalWidth, height * 5 / 8,
                    measuredWidth + totalWidth, height * 3 / 8);

            path2.lineTo(measuredWidth + totalWidth, height);
            path2.lineTo(-measuredWidth + totalWidth, height);
            path2.close();
            return path2;
        }

        @NonNull
        private Path getSinPath(int totalWidth, int center, int start, int end) {
            Path path = new Path();

            path.moveTo(-measuredWidth + totalWidth, measuredHeight * center / 8);

            path.quadTo(-measuredWidth * 7 / 8 + totalWidth, measuredHeight * start / 8,
                    -measuredWidth * 6 / 8 + totalWidth, measuredHeight * center / 8);

            path.quadTo(-measuredWidth * 5 / 8 + totalWidth, measuredHeight * end / 8,
                    -measuredWidth * 4 / 8 + totalWidth, measuredHeight * center / 8);

            path.quadTo(-measuredWidth * 3 / 8 + totalWidth, measuredHeight * start / 8,
                    -measuredWidth * 2 / 8 + totalWidth, measuredHeight * center / 8);

            path.quadTo(-measuredWidth * 1 / 8 + totalWidth, measuredHeight * end / 8,
                    0 + totalWidth, measuredHeight * center / 8);


            path.quadTo(measuredWidth * 1 / 8 + totalWidth, measuredHeight * start / 8,
                    measuredWidth * 2 / 8 + totalWidth, measuredHeight * center / 8);

            path.quadTo(measuredWidth * 3 / 8 + totalWidth, measuredHeight * end / 8,
                    measuredWidth * 4 / 8 + totalWidth, measuredHeight * center / 8);

            path.quadTo(measuredWidth * 5 / 8 + totalWidth, measuredHeight * start / 8,
                    measuredWidth * 6 / 8 + totalWidth, measuredHeight * center / 8);

            path.quadTo(measuredWidth * 7 / 8 + totalWidth, measuredHeight * end / 8,
                    measuredWidth * 8 / 8 + totalWidth, measuredHeight * center / 8);

            path.lineTo(measuredWidth + totalWidth, measuredHeight);
            //  if ()
            path.lineTo(-measuredWidth + totalWidth, measuredHeight);
            path.close();
            return path;
        }

        @NonNull
        private Path getCosPath(int totalWidth, int start, int end) {
            Path path = new Path();

            path.moveTo(measuredWidth + measuredWidth - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth + measuredWidth * 7 / 8 - totalWidth, measuredHeight * start / 8,
                    measuredWidth + measuredWidth * 6 / 8 - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth + measuredWidth * 5 / 8 - totalWidth, measuredHeight * end / 8,
                    measuredWidth + measuredWidth * 4 / 8 - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth + measuredWidth * 3 / 8 - totalWidth, measuredHeight * start / 8,
                    measuredWidth + measuredWidth * 2 / 8 - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth + measuredWidth * 1 / 8 - totalWidth, measuredHeight * end / 8,
                    measuredWidth - totalWidth, measuredHeight * 3 / 8);


            path.quadTo(measuredWidth * 7 / 8 - totalWidth, measuredHeight * start / 8,
                    measuredWidth * 6 / 8 - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth * 5 / 8 - totalWidth, measuredHeight * end / 8,
                    measuredWidth * 4 / 8 - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth * 3 / 8 - totalWidth, measuredHeight * start / 8,
                    measuredWidth * 2 / 8 - totalWidth, measuredHeight * 3 / 8);

            path.quadTo(measuredWidth * 1 / 8 - totalWidth, measuredHeight * end / 8,
                    0 - totalWidth, measuredHeight * 3 / 8);

            path.lineTo(0 - totalWidth, measuredHeight);
            //  if ()
            path.lineTo(2 * measuredWidth + totalWidth, measuredHeight);
            path.close();
            return path;
        }
    }

    private Bitmap getViewBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = null;
        try {
            if (null != view.getDrawingCache()) {
                bitmap = Bitmap.createScaledBitmap(view.getDrawingCache(), 256, 192, false);
            } else {
                bitmap = ((BitmapDrawable) (ContextCompat.getDrawable(context,R.mipmap.bng))).getBitmap();
            }
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } finally {
            view.setDrawingCacheEnabled(false);
            view.destroyDrawingCache();
        }

        return bitmap;
    }
}
